package com.irun.pm.ui;

import java.util.concurrent.Callable;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.irun.pm.R;
import com.irun.pm.application.MyApplication;
import com.irun.pm.interfaces.Callback;
import com.irun.pm.interfaces.IProgressListener;
import com.irun.pm.interfaces.ProgressCallable;
/**
 * 基础类
 * @author Evy
 *
 */
public class BaseActivity extends Activity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTheme(android.R.style.Theme_NoTitleBar);
		View view = this.getWindow().getDecorView();
		view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.grid_background));
	}
	
	protected void showDialog(int id, Object obj) {
		this.obj = obj;
		this.dialog_id = id;
		showDialog(id);
	}

	protected Object obj;
	protected int dialog_id;

	protected String getRstr(int id) {
		return getResources().getString(id);
	}

	protected String getRstr(int id, Object obj) {
		return getResources().getString(id, obj);
	}
	/**
	 * 回调UI处理UI界面数据
	 * @param <T>
	 * @param title  标题
	 * @param message  内容
	 * @param pCallable 回调UI处理复杂操作
	 * @param pCallback 回调结果给UI
	 * @param stopCallback 是否取消操作
	 */
	protected <T> void doProgress(final String title, final String message,
			final Callable<T> pCallable, final Callback<T> pCallback,final Callback<Boolean> stopCallback) {
		final ProgressDialog mProgressDialog = ProgressDialog.show(this, title,
				message);
		mProgressDialog.setOnCancelListener(new OnCancelListener() {
			
			@Override
			public void onCancel(DialogInterface dialog) {
				// TODO Auto-generated method stub
				if(stopCallback != null)
					stopCallback.onCallback(false);
				dialog.cancel();
			}
		});
		((MyApplication) getApplication()).addTaskToFastThreadPool(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					final T result = pCallable.call();
					runOnUiThread(new Runnable() {
						public void run() {
							mProgressDialog.dismiss();
							pCallback.onCallback(result);
						}
					});
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	/**
	 * 带进度处理
	 * @param <T>
	 * @param title 标题
	 * @param message  内容
	 * @param max   进度总长度
	 * @param pCallback  结束后返回UI
	 * @param stopCallback  终止操作回调
	 * @param pCallable  UI发送进度更改
	 */
	protected <T> void doProgressHor(final String title, final String message,int max,final ProgressCallable<T> pCallable,
			final Callback<T> pCallback,final Callback<Boolean> stopCallback) {
		final ProgressDialog pdialog = new ProgressDialog(this);
		pdialog.setTitle(title);
		pdialog.setMessage(message);
		pdialog.setIndeterminate(false);
		pdialog.setCancelable(false);
		pdialog.setMax(max);
		pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		pdialog.setButton("取消", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				if(stopCallback != null)
					stopCallback.onCallback(true);
				dialog.cancel();
				Toast.makeText(getBaseContext(), "取消操作", Toast.LENGTH_SHORT).show();
			}
		});
		pdialog.show();
		((MyApplication) getApplication()).addTaskToFastThreadPool(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					final T result = pCallable.call(new IProgressListener() {
						
						@Override
						public void onProgressChanged(final int pProgress) {
							// TODO Auto-generated method stub
							//改变progress
							runOnUiThread(new Runnable() {
								public void run() {
									pdialog.setProgress(pProgress);
								}
							});
						}
					});
					runOnUiThread(new Runnable() {
						public void run() {
							pdialog.dismiss();
							pCallback.onCallback(result);
						}
					});
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * 异步操作
	 * @param <T>
	 * @param pCallable
	 * @param pCallback
	 */
	protected <T> void doAsyn(final Callable<T> pCallable, final Callback<T> pCallback) {
		((MyApplication) getApplication()).addTaskToFastThreadPool(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					final T result = pCallable.call();
					pCallback.onCallback(result);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
}
